unit inversor_presup;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, Grids, DBGrids, DBCtrls, ExtCtrls, StdCtrls, Buttons, jpeg;

type
  TfrmInversorPresup = class(TForm)
    sInversorPre: TDataSource;
    Panel1: TPanel;
    grdInversorPresup: TDBGrid;
    Panel62: TPanel;
    nvgInversor: TDBNavigator;
    Panel56: TPanel;
    Panel55: TPanel;
    btnGrabarInversor: TBitBtn;
    Panel27: TPanel;
    Image4: TImage;
    Panel28: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure sInversorPreStateChange(Sender: TObject);
    procedure btnGrabarInversorClick(Sender: TObject);
    procedure nvgInversorClick(Sender: TObject; Button: TNavigateBtn);
    procedure FormCreate(Sender: TObject);
    procedure Panel41Click(Sender: TObject);
    procedure Label1Click(Sender: TObject);
    procedure Label2Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    Fid: word;
    procedure Setid(const Value: word);
    { Private declarations }
  published
    property id: word read Fid write Setid;
  public
    constructor Entrar(Aowner: TComponent; usuario: word; idObra: integer);
  end;

var
  frmInversorPresup: TfrmInversorPresup;
  totalPresupuesto : Double;


implementation

uses MDatos, Recursos, abm;

{$R *.dfm}

constructor TfrmInversorPresup.Entrar(Aowner: TComponent; usuario: word; idObra: integer);
var
  sql: string;
begin
  inherited Create (Aowner);
  self.id := usuario;
  try
    sql := 'SELECT denominacion, total, ganancia, iva, alicuota_mat, subt_mat, solo_mo, id_presupuesto FROM presupuesto WHERE id_presupuesto = '+IntToStr(idObra);
    EjecutarSql(Datos.qryVar5, sql);
    totalPresupuesto := Datos.qryVar5.Fields [1].AsFloat;
    caption := 'Inversores para "' + Datos.qryVar5.Fields [0].AsString + '"';
    Datos.cInversorPresup.Params [0].AsInteger := idObra;
    Datos.cInversorPresup.Open;
    btnGrabarInversor.Enabled := (Datos.cInversorPresup.ChangeCount > 0);
    ShowModal;
  finally
    Datos.cInversorPresup.Close;
  end;
end;

procedure TfrmInversorPresup.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
  CanClose := Datos.AntesdeCerrar(Datos.cInversorPresup)=True; 
end;

procedure TfrmInversorPresup.sInversorPreStateChange(Sender: TObject);
begin
  btnGrabarInversor.Enabled := (Datos.cInversorPresup.ChangeCount > 0);
end;

procedure TfrmInversorPresup.btnGrabarInversorClick(Sender: TObject);
begin
  if (Datos.cInversorPresup.State in dsEditModes) then
    Datos.cInversorPresup.Post;
  if Datos.cInversorPresup.ChangeCount > 0 then
    if (Datos.cInversorPresup.ApplyUpdates(0) > 0) then
      Application.MessageBox('Error al grabar en tabla Inversores por Presupuesto', 'Error', mb_Ok+mb_IconError)
    else
      sInversorPreStateChange(Sender);
end;

procedure TfrmInversorPresup.nvgInversorClick(Sender: TObject;
  Button: TNavigateBtn);
begin
  if Button in [nbDelete] then
    sInversorPreStateChange(Sender);
end;

procedure TfrmInversorPresup.FormCreate(Sender: TObject);
begin
  Acomodo(frmInversorPresup);
end;

procedure TfrmInversorPresup.Panel41Click(Sender: TObject);
begin
  Close;
end;

procedure TfrmInversorPresup.Label1Click(Sender: TObject);
begin
  //TfrmABM.EntrarSeleccionando(Sender, 7);
  TfrmABM.Entrar(self, self.id, 7);
  if Datos.cInversor.State in [dsInactive] then
    Datos.cInversor.Open
  else Datos.cInversor.Refresh;
end;

procedure TfrmInversorPresup.Label2Click(Sender: TObject);
begin
  Close;
end;

procedure TfrmInversorPresup.Setid(const Value: word);
begin
  Fid := Value;
end;

procedure TfrmInversorPresup.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action := caFree;
end;

end.
